package com.mcxx.modules.miniSalvation.family.service.read;

import com.mcxx.common.exception.BizException;
import com.mcxx.constant.DictConstant;
import com.mcxx.modules.base.service.read.DictionaryReadService;
import com.mcxx.modules.miniSalvation.family.dao.read.MemberAssetsReadDAO;
import com.mcxx.modules.miniSalvation.family.dto.MemberAssetsDTO;
import com.mcxx.modules.miniSalvation.family.entity.FamilyMemberEntity;
import com.mcxx.modules.miniSalvation.family.entity.MemberAssetsEntity;
import com.mcxx.util.Constant;
import com.mcxx.util.FailureCode;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import com.yinhai.ta404.module.dict.entity.TaDictPo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
@TaTransactional(readOnly = true)
public class MemberAssetsReadService {
    @Autowired
    private MemberAssetsReadDAO memberAssetsReadDAO;
    @Autowired
    private DictionaryReadService dictionaryReadService;
    @Autowired
    private FamilyMemberReadService familyMemberReadService;



    public List<MemberAssetsEntity> queryList(MemberAssetsEntity entity){
        return memberAssetsReadDAO.queryList(entity);
    }


    public List<MemberAssetsEntity> queryByFamilyIdAndType(String familyId, List<String> assetsTypeList){
        return memberAssetsReadDAO.queryByFamilyIdAndType(familyId, assetsTypeList);
    }


    public List<MemberAssetsDTO> queryListByFamilyId(String familyId, String orderId, String assetsType){
        List<MemberAssetsDTO> memberAssetsDTOList = memberAssetsReadDAO.queryListByFamilyId(familyId, orderId,assetsType);
        for(MemberAssetsDTO dto : memberAssetsDTOList){
            TaDictPo dictPo = dictionaryReadService.getCodeCache(DictConstant.ASSETSTYPE, dto.getAssetsType().substring(0,2));
            StringBuffer assetsTypeStr = new StringBuffer(dictPo.getLabel()).append("-");
            dictPo = dictionaryReadService.getCodeCache(DictConstant.ASSETSTYPE, dto.getAssetsType());
            assetsTypeStr.append(dictPo.getLabel());

            dto.setAssetsType(assetsTypeStr.toString());
        }
        return  memberAssetsDTOList;
    }




    public List<MemberAssetsDTO> getMemberAssetsList(String familyId, String orderId, String assetsType){
        return memberAssetsReadDAO.getMemberIncomeList(familyId, orderId, assetsType);
    }

    public Map<String,Object> getMemberIncomeList(String familyId, String orderId, String type){

        Map<String,Object> res = new HashMap<>();

        List<MemberAssetsDTO> memberIncomeDTOList = memberAssetsReadDAO.getMemberIncomeList(familyId, orderId, Constant.MemberAssetsType.SHOU_RU);

        List<MemberAssetsDTO> memberAsDTOList = memberAssetsReadDAO.getMemberIncomeList(familyId, orderId,Constant.MemberAssetsType.DI_KOU_XIANG);
        if("1".equals(type)){

            res.put("income",memberIncomeDTOList);
            res.put("assets",memberAsDTOList);
        }else if("2".equals(type)){

            List<FamilyMemberEntity> memberList = familyMemberReadService.listNormalMemberByFamilyId(familyId);
            List<MemberAssetsDTO> incomeList = new ArrayList<>();
            List<MemberAssetsDTO> assetsList = new ArrayList<>();
            Map<String, MemberAssetsDTO> inMap = memberIncomeDTOList.stream().collect(Collectors.toMap(MemberAssetsDTO::getMemberId, a -> a,(k1, k2)->k1));
            Map<String, MemberAssetsDTO> asMap = memberAsDTOList.stream().collect(Collectors.toMap(MemberAssetsDTO::getMemberId, a -> a,(k1, k2)->k1));
            if(!CollectionUtils.isEmpty(memberList)){
                for(FamilyMemberEntity m:memberList){
                    MemberAssetsDTO in = new MemberAssetsDTO();
                    in.setMemberId(m.getId());
                    in.setMemberName(m.getName());
                    MemberAssetsDTO memberAssetsDTO = inMap.get(m.getId());
                    if(null != memberAssetsDTO){
                        in.setOne(memberAssetsDTO.getOne());
                        in.setTwo(memberAssetsDTO.getTwo());
                        in.setThree(memberAssetsDTO.getThree());
                        in.setFour(memberAssetsDTO.getFour());
                        in.setFive(memberAssetsDTO.getFive());
                        in.setCountPrice(memberAssetsDTO.getCountPrice());
                    }
                    incomeList.add(in);
                    MemberAssetsDTO as = new MemberAssetsDTO();
                    as.setMemberId(m.getId());
                    as.setMemberName(m.getName());
                    MemberAssetsDTO memberAssetsDTO1 = asMap.get(m.getId());
                    if(null != memberAssetsDTO1){
                        as.setOne(memberAssetsDTO1.getOne());
                        as.setTwo(memberAssetsDTO1.getTwo());
                        as.setThree(memberAssetsDTO1.getThree());
                        as.setFour(memberAssetsDTO1.getFour());
                        as.setCountPrice(memberAssetsDTO1.getCountPrice());
                    }
                    assetsList.add(as);
                }
                res.put("income",incomeList);
                res.put("assets",assetsList);
            }else{
                throw new BizException("-1","系统异常(未获取到成员信息),请联系管理员处理");
            }

        }
        String incomeRemark = memberAssetsReadDAO.getAssetsRemark(familyId, orderId, Constant.MemberAssetsType.SHOU_RU_BEIZHU);
        String assetsRemark = memberAssetsReadDAO.getAssetsRemark(familyId, orderId, Constant.MemberAssetsType.ZHICHU_BEIZHU);
        res.put("incomeRemark",incomeRemark);
        res.put("assetsRemark",assetsRemark);
        return  res;
    }


    public List<MemberAssetsEntity> queryListByFamilyIds(String familyId, String orderId) {
        return memberAssetsReadDAO.queryListByFamilyIds(familyId, orderId);
    }


    public MemberAssetsEntity getById(String id){
        return memberAssetsReadDAO.queryById(id);
    }


    public int cntMemberAssetsNotProperty(MemberAssetsEntity memberAssetsEntity){
        if(memberAssetsEntity.getAssetsType().startsWith(Constant.MemberAssetsType.CAI_CHAN)){

            return 0;
        }
        int rst = this.cntMemberAssets(memberAssetsEntity);
        if(rst > 0){
            throw new BizException(FailureCode.ERR_20065);
        }
        return rst;
    }


    public int cntMemberAssets(MemberAssetsEntity memberAssetsEntity){
        return memberAssetsReadDAO.cntMemberAssets(memberAssetsEntity);
    }


    public Map<String, BigDecimal> sumMemberAssets(String familyId, String orderId){
        return memberAssetsReadDAO.sumMemberAssets(familyId, orderId);
    }


    public int cntOrderAssets(String orderId){
        return memberAssetsReadDAO.cntOrderAssets(orderId);
    }


    public List<MemberAssetsDTO> cntOrderAssetsList(List<String> orderIdList){
        return memberAssetsReadDAO.cntOrderAssetsList(orderIdList);
    }


    public String getAssetsRemark(String familyId, String orderId ,String assetsType){
        return memberAssetsReadDAO.getAssetsRemark(familyId, orderId,assetsType);
    }
}
